<?php

declare (strict_types=1);
namespace App\Model;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Cache\Annotation\CacheEvict;
use Hyperf\Database\Model\SoftDeletes;
/**
 * @property int $id 主键
 * @property int $mid 机构id
 * @property int $dept_id 部门ID
 * @property string $terminal_number 绑定gps设备终端号
 * @property string $vehicle_photo 车辆照片
 * @property string $vehicle_name 车辆名称
 * @property string $vehicle_type 车辆类型
 * @property string $vehicle_brand 车辆品牌
 * @property string $vehicle_model 车辆型号
 * @property string $vehicle_capacity 车辆座位数
 * @property int $vehicle_status 车辆状态
 * @property string $registration_number 车辆注册号
 * @property string $purchase_date 车辆购买日期
 * @property int $status 状态
 * @property string $remark 备注
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property string $deleted_at 软删除字段
 */
class Vehicle extends BaseModel
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'vehicles';

    protected $hidden = ['deleted_at'];
    
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'mid', 'dept_id', 'device_id','device_staus','lng', 'lat','plate_number', 'vehicle_photo', 'vehicle_name', 'vehicle_type', 'vehicle_brand', 'vehicle_model', 'vehicle_capacity', 'vehicle_status', 'registration_number', 'purchase_date','last_licheng','is_warn', 'status', 'remark', 'created_at', 'updated_at', 'deleted_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'mid' => 'integer', 'dept_id' => 'integer','device_id' => 'integer','device_staus' => 'integer','vehicle_capacity' => 'integer', 'vehicle_status' => 'integer', 'is_warn' => 'integer',  'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
    /**
     * [createuser 创建者]
     * @return [type] [description]
     */
    public function createuser()
    {
        return $this->hasOne(User::class, 'id', 'created_user');
    }
    /**
     * [device gps定位设备]
     * @return [type] [description]
     */
    public function device()
    {
        return $this->hasOne(GpsDevice::class, 'id', 'device_id');
    }
    /**
     * [fleet 车队]
     * @return [type] [description]
     */
    public function fleet()
    {
        return $this->belongsToMany(Fleet::class,'fleets_vehicles','vid','fid');
    }
    /**
     * [diaodu_record 调度记录]
     * @return [type] [description]
     */
    public function diaodu_record()
    {
        return $this->hasOne(VehicleDriver::class, 'car_id', 'id');
    }
    /**
     * [baoyang 保养记录]
     * @return [type] [description]
     */
    public function baoyang()
    {
        return $this->hasOne(VehicleMaintenanceRecord::class, 'car_id', 'id');
    }
    /**
     * [jiayou 加油记录]
     * @return [type] [description]
     */
    public function jiayou()
    {
        return $this->hasOne(VehicleAddoilRecord::class, 'car_id', 'id');
    }
    /**
     * [baoxian 保险记录]
     * @return [type] [description]
     */
    public function baoxian()
    {
        return $this->hasOne(VehicleInsurance::class, 'car_id', 'id');
    }
    /**
     * [nianjian 年检记录]
     * @return [type] [description]
     */
    public function nianjian()
    {
        return $this->hasOne(VehicleInspectionRecord::class, 'car_id', 'id');
    }
    /**
     * [weizhang 违章记录]
     * @return [type] [description]
     */
    public function weizhang()
    {
        return $this->hasOne(VehicleViolationRecord::class, 'car_id', 'id');
    }
    /**
     * [shigu 事故记录]
     * @return [type] [description]
     */
    public function shigu()
    {
        return $this->hasOne(VehicleAccident::class, 'car_id', 'id');
    }
    /**
     * [lnglat 经纬度记录]
     * @return [type] [description]
     */
    public function lnglat()
    {
        return $this->hasOne(VehicleLnglatRecord::class, 'car_id', 'id');
    }
    /**
     * [locationRecords 经纬度记录]
     * @return [type] [description]
     */
    public function locationRecords()
    {
        return $this->hasMany(VehicleLnglatRecord::class, 'car_id', 'id');
    }
    /**
     * [org 机构]
     * @return [type] [description]
     */
    public function org()
    {
        return $this->hasOne(Institution::class, 'id', 'mid');
    }
    /**
     * [getCarArr description]
     * @param  [type] $user [description]
     * @return [type]       [description]
     * @Cacheable(prefix="AllCar",ttl=60, value="#{where.mid}",listener="AllCar")
     */
    public static function getCarArr($where = []):array
    {
        $M = self::query()->with('org');
        if(isset($where['mid'])) $M->where('mid',$where['mid']);
        if(isset($where['key'])) $M->where('plate_number', 'like', '%'.$where['key'].'%');
        $list = $M->orderBy('id','desc')->get();
        $list->map(function($item){
            $item['name'] = $item['vehicle_name'].'['.$item['plate_number'].']';
        });
        return $list->toArray();
    }
}